package com.woniuxy.dudugua.controller;

import com.woniuxy.dudugua.entity.Address;
import com.woniuxy.dudugua.entity.Order;
import com.woniuxy.dudugua.entity.OrderInfo;
import com.woniuxy.dudugua.entity.User;
import com.woniuxy.dudugua.service.CartService;
import com.woniuxy.dudugua.service.OrderService;
import com.woniuxy.dudugua.service.UserCouponsService;
import com.woniuxy.dudugua.util.Result;
import com.woniuxy.dudugua.util.view.CouponsView;
import com.woniuxy.dudugua.util.view.CreateOrderView;
import com.woniuxy.dudugua.util.view.UserCouponsView;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private CartService cartService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UserCouponsService userCouponsService;
    @RequestMapping("getcoupons")
    public Result getCoupons(Integer[] skuid){
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        List<UserCouponsView> list = orderService.selectUserConponsViewCanUse(user.getId(), skuid);
        return new Result("200","success",null,list);
    }
    @PostMapping("creatorder")
    public Result creatOrder(@RequestBody CreateOrderView createOrderView){
        if(createOrderView.getUserCouponsView() != null){
            System.out.println(createOrderView.getUserCouponsView().toString());
        }
        System.out.println(createOrderView.getAddress().toString());
        List<OrderInfo> orderlist = createOrderView.getOrderlist();
        for(OrderInfo o : orderlist){
            System.out.println(o.toString());
        }
        User user = (User)SecurityUtils.getSubject().getPrincipal();
        int i = orderService.insertOrder(createOrderView.getOrderlist(), createOrderView.getAddress(), createOrderView.getUserCouponsView() == null ? null : createOrderView.getUserCouponsView(), user.getId());
        String key="order="+i;
        redisTemplate.opsForValue().set(key,"123",60*15, TimeUnit.SECONDS);
        //修改优惠券状态
        if(createOrderView.getUserCouponsView() != null){
            userCouponsService.updateStatusById(createOrderView.getUserCouponsView().getUserCouponsId(),2);
        }
        return new Result("200","success",i,null);
    }
    @RequestMapping("deleteByBuy")
    public Result deleteCartByBuy(Integer[] cartId){
        if(cartId != null && cartId.length != 0){
            for (int i = 0; i < cartId.length; i++) {
                cartService.deleteCart(cartId[i]);
            }
            return new Result("200","success",null,null);
        }
        return null;
    }
    @RequestMapping("getorder")
    public Result getOrder(Integer orderid){
        Order order = orderService.selectOrderById(orderid);
        return new Result("200","success",order,null);
    }
    @RequestMapping("getuser")
    public Result getUser(){
        User user = (User)SecurityUtils.getSubject().getPrincipal();
        return new Result("200","success",user,null);
    }
    @RequestMapping("paysuccess")
    public Result paySuccess(Integer orderid){
        orderService.updateStatus(orderid,2);
        return new Result("200","success",null,null);
    }
}
